package com.nicy.dream.biz;

import com.nicy.dream.form.UserForm;
import com.nicy.dream.service.SayHelloService;
import com.nicy.dream.utils.RedisLockTemplate;
import com.nicy.dream.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "Hello测试控制器")
@CrossOrigin
@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    SayHelloService sayHello;

    @GetMapping("/nicy")
    @ApiOperation(value = "SayHello方法")
    @ApiImplicitParam(name = "name",value = "姓名",type = "String",required = true)
    public Map<String,String> SayHello(@RequestParam(name = "name",required = true) String name){
        Map<String,String> map = new HashMap<>();
        map.put("hello","hello".concat(name));
        return map;
    }


    @GetMapping(value = "sayHello2")
    @ApiOperation(value = "sayHello2方法")
    @ApiImplicitParam(name = "name",value = "姓名",type = "String",required = true)
    public Map<String,String> SayHello2(@RequestParam(name = "name",required = true) String name){
        Map<String,String> map = new HashMap<>();
        map.put("hello",sayHello.sayHello2(name));
        return map;
    }


    @GetMapping("/morn")
    @ApiOperation(value = "SayMorn方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "count",dataType = "int",value = "数量"),
            @ApiImplicitParam(name = "who",dataType = "String",value = "名字")
    })
    public Map<String,String> SayMorn(Integer count,String who){
        Map<String,String> map = new HashMap<>();
        map.put("hello","hello，一共".concat(count.toString()).concat("位"+who));
        return map;
    }

    @GetMapping("/user")
    @ApiOperation(value = "获取用户信息")
    public Map<String, UserForm> getUserInfo(){
        //创建用户信息
        UserForm userForm = UserForm.builder()
                .name("NicyYe")
                .age("18")
                .sex("男")
                .phone("18932376991")
                //.birthday(LocalDateTime.now())
                .build();

        //添加测试

        //创建容器
        Map<String,UserForm> map = new HashMap<>();
        //封装返回数据
        map.put("user",userForm);
        //返回值
        return map;
    }

    @ApiOperation(value = "hello测试方法")
    @GetMapping("/hello")
    public String index(){
        return "Hello World";
    }


    @Autowired
    RedisLockTemplate redisLockTemplate;

    @ApiOperation(value = "redis锁测试")
    @GetMapping("/addUser")
    public Map<String,String> addUser(UserForm userForm){

        Map<String,String> map = new HashMap<>();

//        Result result = redisLockTemplate.execute("addUser.".concat(userForm.getName()),5000L,() -> {
//            System.out.println();
//            //Result result2 =
//            return Result.ok("添加成功");
//        });

        //Result result2 = redisLockTemplate.execute("addUser.".concat(userForm.getName()),5000L,() -> {
            //System.out.println();
            //Result result2 =
            //return Result.ok("添加成功");
        //});
        //System.out.println(result2);
        //map.put("msg",result2.toString());
        return map;
    }


    @ApiOperation(value = "查询我的用户信息")
    @PostMapping("/queryMyUser")
    public Map<String,String> queryMyInfo(@RequestBody UserForm userForm){
        System.out.println(userForm.getName());
        return null;
    }

}
